package handlers

import (
	"219yk/api/requset"
	"219yk/api/response"
	"219yk/dao"
	"219yk/models"
	"219yk/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"path/filepath"
)

func AddVideo(c *gin.Context) {
	var data requset.AddVideo
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	video := models.Video{
		UserId:  int(c.GetUint("userId")),
		Title:   data.Title,
		Context: data.Context,
		CType:   data.CType,
	}
	err = dao.AddVideo(video)
	if err != nil {
		response.ResponseError(c, "发布失败")
		return
	}
	response.ResponseSuccess(c, "发布成功")
}

func ConCern(c *gin.Context) {
	var data requset.ConCern
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	ren := models.ConRen{
		UserId:       int(c.GetUint("userId")),
		ConRenUserId: data.ConRenUserId,
	}
	err = dao.AddConRen(ren)
	if err != nil {
		fmt.Println("关注失败")
		return
	}
	response.ResponseSuccess(c, "关注成功")
}

func Good(c *gin.Context) {
	var data requset.Good
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if data.GoodToNoGood == 1 {
		AddGood(data, c)
	} else if data.GoodToNoGood == 2 {
		DeletedGood(data, c)
	}
}

func AddGood(data requset.Good, c *gin.Context) {
	if !dao.GetVideo(data.VideoId) {
		response.ResponseError(c, "动态不存在")
		return
	}
	good := models.Good{
		UserId:     int(c.GetUint("userId")),
		VideoId:    data.VideoId,
		GoodUserId: data.GoodUserId,
		GoodToGood: 1,
	}
	if !dao.CreateGood(good) {
		response.ResponseError(c, "点赞失败")
	}
	dao.AddVideoGood(data.VideoId)
	response.ResponseSuccess(c, "点赞成功")
}

func DeletedGood(data requset.Good, c *gin.Context) {
	if !dao.GetVideo(data.VideoId) {
		response.ResponseError(c, "动态不存在")
		return
	}
	good := models.Good{
		UserId:     int(c.GetUint("userId")),
		VideoId:    data.VideoId,
		GoodUserId: data.GoodUserId,
		GoodToGood: 1,
	}
	if !dao.DeletedGood(good) {
		response.ResponseError(c, "取消点赞失败")
	}
	dao.DelVideoGood(data.VideoId)
	response.ResponseSuccess(c, "取消点赞成功")
}

func Comment(c *gin.Context) {
	var data requset.Comment
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}

	if data.FatherId > 0 {
		CommentToVideo(c, data)
	} else {
		CommentToComment(c, data)
	}

}

func CommentToComment(c *gin.Context, data requset.Comment) {
	if !dao.GetVideo(data.VideoId) {
		response.ResponseError(c, "动态不存在")
		return
	}
	comment := models.Comment{
		Context:  data.Context,
		UserId:   int(c.GetUint("userId")),
		VideoId:  data.VideoId,
		FatherId: data.FatherId,
	}
	if !dao.CreateComment(comment) {
		response.ResponseError(c, "评论失败")
	}
	dao.CreateCommentNum(data.VideoId)
	response.ResponseSuccess(c, "评论成功")
}

func CommentToVideo(c *gin.Context, data requset.Comment) {
	if !dao.GetVideo(data.VideoId) {
		response.ResponseError(c, "动态不存在")
		return
	}
	comment := models.Comment{
		Context: data.Context,
		UserId:  int(c.GetUint("userId")),
		VideoId: data.VideoId,
	}
	if !dao.CreateComment(comment) {
		response.ResponseError(c, "评论失败")
	}
	dao.CreateCommentNum(data.VideoId)
	response.ResponseSuccess(c, "评论成功")
}

func List(c *gin.Context) {
	video, err := dao.ListVideo()
	if err != nil {
		response.ResponseError(c, "查询失败")
		return
	}
	var lists []*models.Video
	for _, list := range video {
		lists = append(lists, &models.Video{
			Model:      gorm.Model{ID: list.ID},
			UserId:     list.UserId,
			Title:      list.Title,
			Context:    list.Context,
			GoodNum:    list.GoodNum,
			CommentNum: list.CommentNum,
			CType:      list.CType,
		})
	}
	response.ResponseSuccess(c, lists)
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.ResponseError(c, "文件获取失败")
		return
	}
	ext := filepath.Ext(file.Filename)
	end := map[string]bool{
		".png":  true,
		".jpg":  true,
		".map4": true,
	}
	if !end[ext] {
		response.ResponseError(c, "文件类型错误")
		return
	}
	if file.Size >= 500*1024*1024 {
		response.ResponseError(c, "文件过大")
		return
	}
	dst := "C:\\Users\\86199\\Desktop\\219yk\\startic\\upload" + file.Filename
	c.SaveUploadedFile(file, dst)
	pkg.Upload(dst, file.Filename)
	response.ResponseSuccess(c, pkg.Urlt(file.Filename))
}

func History(c *gin.Context) {
	var data requset.History
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if data.CommonId == "good" {
		HistroyGoodList(data, c)
	} else {
		HistoryCommentList(data, c)
	}
}

func HistoryCommentList(data requset.History, c *gin.Context) {
	list, err := dao.CommentList()
	if err != nil {
		response.ResponseError(c, "查询失败")
		return
	}
	var lists []*models.Comment
	for _, comment := range list {
		lists = append(lists, &models.Comment{
			Model:    gorm.Model{ID: comment.ID},
			Context:  comment.Context,
			UserId:   comment.UserId,
			VideoId:  comment.VideoId,
			FatherId: comment.FatherId,
		})
	}
	response.ResponseSuccess(c, lists)
}

func HistroyGoodList(data requset.History, c *gin.Context) {
	list, err := dao.GoodList()
	if err != nil {
		response.ResponseError(c, "查询失败")
		return
	}
	var lists []*models.Good
	for _, good := range list {
		lists = append(lists, &models.Good{
			Model:      gorm.Model{ID: good.ID},
			UserId:     good.UserId,
			VideoId:    good.VideoId,
			GoodUserId: good.GoodUserId,
			GoodToGood: good.GoodToGood,
		})
	}
	response.ResponseSuccess(c, lists)
}
